
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

public class MenuScreen extends GameCanvas implements Runnable {

    private int delay = 200;
    private String[] options = {"New Game", "OptionSound", "Help", "High Score", "About", "Exit"};
    private Point locationMenu = new Point(45, 30);
    private int itemWidth;
    private int item_space = 25;
    private Image itemSelect;
    private Image itemUnselect;
    private Image backGround;
    private Graphics g;
    private int itemHeight;
    private int currentIndex = 0;
    private int white = 0xffffff;
    private int black = 0x000000;
    private Font font;
    private boolean isActive;
    private Main iMain;
    private PlayGame iPlayGame;
    private OptionScreen iOptionScreen;
    private HelpScreen iHelpScreen;
    private AboutScreen iAboutScreen;
    private HighScoreScreen iHighScoreScreen;
    private ExitScreen iExitScreen;
    private Player clickSound;
    private Player updownSound;

    public MenuScreen(Main aMain) {
        super(true);
        setFullScreenMode(true);
        iMain = aMain;
    }

    public void Start() {
        try {

            this.g = this.getGraphics();
            itemSelect = Image.createImage("butoption_Press.png");
            itemUnselect = Image.createImage("butoption_unPress.png");
            backGround = Image.createImage("Pikachu_Menu.png");
            this.itemHeight = itemSelect.getHeight();
            this.itemWidth = itemUnselect.getWidth();
            this.font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
            this.locationMenu = new Point(this.getWidth() / 2 - this.itemWidth / 2, this.locationMenu.getY());

            g.setFont(font);
        } catch (Exception e) {
        };

        try {
            //moveSound = Manager.c
            InputStream in1 = getClass().getResourceAsStream("/updown.wav");
            updownSound = Manager.createPlayer(in1, "audio/x-wav");
            InputStream in2 = getClass().getResourceAsStream("/click.wav");
            clickSound = Manager.createPlayer(in2, "audio/x-wav");
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        isActive = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        Draw();
        while (isActive) {
            processKeys(getKeyStates());

            flushGraphics();
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    // load lai man hinh
    public void Draw() {
        g.drawImage(backGround, 0, 0, Graphics.LEFT | Graphics.TOP);
        for (int i = 0; i < options.length; i++) {
            if (currentIndex == i) {
                g.drawImage(itemSelect, locationMenu.getX(),
                        locationMenu.getY() + i * itemHeight + item_space, Graphics.TOP | Graphics.LEFT);
            } else {
                g.drawImage(itemUnselect, locationMenu.getX(),
                        locationMenu.getY() + i * itemHeight + item_space, Graphics.TOP | Graphics.LEFT);
            }
            g.drawString((i + 1) + ". " + options[i], locationMenu.getX() + 15,
                    locationMenu.getY() + i * itemHeight + item_space + 10, Graphics.TOP | Graphics.LEFT);

            this.flushGraphics();
        }
    }

    //  item(control) ko dc chon tro lai binh thuong
    private void clear_Item(int i) {
        g.drawImage(itemUnselect, locationMenu.getX(),
                locationMenu.getY() + i * itemHeight + item_space, Graphics.TOP | Graphics.LEFT);
        g.setColor(this.black);
        g.drawString((i + 1) + ". " + options[i], locationMenu.getX() + 15,
                locationMenu.getY() + i * itemHeight + item_space + 10, Graphics.TOP | Graphics.LEFT);

        flushGraphics();
    }

    // draw item(control) duoc chon
    private void Draw_nextItem(int i) {
        g.drawImage(itemSelect, locationMenu.getX(),
                locationMenu.getY() + i * itemHeight + item_space, Graphics.TOP | Graphics.LEFT);
        g.setColor(this.black);
        g.drawString((i + 1) + ". " + options[i], locationMenu.getX() + 15,
                locationMenu.getY() + i * itemHeight + item_space + 10, Graphics.TOP | Graphics.LEFT);
        flushGraphics();
    }

    // Dat lai vi tri control
    public void processKeys(int keyCode) {
        switch (keyCode) {
            case UP_PRESSED: {
                clear_Item(currentIndex);
                menuSelectChange(-1);
                Draw_nextItem(currentIndex);
                if (OptionScreen.getChon() == 1) {
                    try {
                        updownSound.start();
                    } catch (MediaException ex) {
                        ex.printStackTrace();
                    }
                }
            }
            break;

            case DOWN_PRESSED: {
                clear_Item(currentIndex);
                menuSelectChange(1);
                Draw_nextItem(currentIndex);

                if (OptionScreen.getChon() == 1) {
                    try {
                        updownSound.start();
                    } catch (MediaException ex) {
                        ex.printStackTrace();
                    }
                }
            }
            break;

            case FIRE_PRESSED: {
                XuLy();
                if (OptionScreen.getChon() == 1) {
                    try {
                        clickSound.start();
                    } catch (MediaException ex) {
                        ex.printStackTrace();
                    }
                }
            }
            break;
        }
    }

    public void XuLy() {
        switch (currentIndex) {
            //{"New Game", "Option" ,"Help", "High Score", "About", "Exit"};
            case 0:
                iPlayGame = new PlayGame(iMain, this);
                iPlayGame.Start();
                Display.getDisplay(iMain).setCurrent(iPlayGame);
                break;
            case 1:
                iOptionScreen = new OptionScreen(iMain, this);
                iOptionScreen.Start();
                Display.getDisplay(iMain).setCurrent(iOptionScreen);
                break;
            case 2:
                iHelpScreen = new HelpScreen(iMain, this);
                iHelpScreen.Start();
                Display.getDisplay(iMain).setCurrent(iHelpScreen);
                break;
            case 3:
                iHighScoreScreen = new HighScoreScreen(iMain, this);
                iHighScoreScreen.Start();
                Display.getDisplay(iMain).setCurrent(iHighScoreScreen);
                break;
            case 4:
                iAboutScreen = new AboutScreen(iMain, this);
                iAboutScreen.Start();
                Display.getDisplay(iMain).setCurrent(iAboutScreen);
                break;
            case 5:
                iExitScreen = new ExitScreen(iMain);
                Display.getDisplay(iMain).setCurrent(iExitScreen);
                break;
        }
    }

    private void menuSelectChange(int h) {
        //viec lua chon menu se chay vong tron
        currentIndex += h + options.length;
        currentIndex = currentIndex % options.length;
    }

    public void stop() {
        isActive = false;
    }
}
